package com.example.demo;

import com.google.common.collect.Lists;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.List;

public class ApiToken {

    private String timestamp;
    private String accessKey;
    private String secretKey;
    private String randomStr;

    public ApiToken() {
    }

    public ApiToken(String timestamp, String accessKey, String secretKey, String randomStr) {
        this.timestamp = timestamp;
        this.accessKey = accessKey;
        this.secretKey = secretKey;
        this.randomStr = randomStr;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getAccessKey() {
        return accessKey;
    }

    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public String getRandomStr() {
        return randomStr;
    }

    public void setRandomStr(String randomStr) {
        this.randomStr = randomStr;
    }

    @Override
    public String toString() {
        return createToken(timestamp, accessKey, secretKey, randomStr);
    }

    private String createToken(String timestamp, String accessKey, String secretKey, String randomStr) {
        List<String> signList = Lists.newArrayList(timestamp, secretKey, accessKey, randomStr);
        Collections.sort(signList);
        return DigestUtils.sha1Hex(StringUtils.join(signList, "|"));
    }
}
